<?php

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/10/13
 * Time: 17:53
 */

namespace app\admin\model;


use think\Model;

class Article extends Model
{
    // 图片json解析

    public function getImagesAttr($value)
    {
        $images = json_decode($value);
        if (is_array($images)) {
            return $images;
        }
        return [];
    }

    // 图片json解析
    public function getContentAttr($value)
    {
        $content = json_decode($value);
        if (is_array($content)) {
            return $content;
        }
        return [];
    }

    // 分类名称对象
    public function getCategoriesAttr($value)
    {
        if (empty($value) || $value <= 0) {
            return [];
        }
        $db = db('article_categories');
        $data = $db->field('id,name')->find($value);
        return $data;
    }

    public function getContentSizeAttr($value)
    {
        $contents = json_decode($value);
        if (is_array($contents)) {
            $size = count($contents);
        } else {
            $size = 0;
        }
        return $size;
    }

    public function getLinesArticleAttr($value)
    {
        return model('Article')->field('id,title,images')->where('id', 'in', json_decode($value))->select();
    }

    public function getStartNameAttr($value)
    {
        $startId = json_decode($value)[0];
        return db('article')->find($startId)['name'];
    }

    public function getLinesCountAttr($value)
    {
        return count(json_decode($value));
    }

}